package com.example.baselib.imageLoader;

import com.example.baselib.utils.log.LogUtils;
import ohos.agp.components.Image;
import ohos.app.Context;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:  TODO
 * Author: admin
 * CreateDate: 2021/4/8 9:45
 */
public class ImageLoader {

    private Map<String, RequestBuilder> builderMap;
    private static final ImageLoader mImageLoader = new ImageLoader();

    private ImageLoader() {
        builderMap = new HashMap<>();
    }

    public static ImageLoader getInstance() {
        return mImageLoader;
    }

    public void cancelLoad(Object obj) {
        String key = getKey(obj);
        if (null != key && key.length() > 0) {
            RequestBuilder builder = builderMap.get(key);
            if (builder != null
                    && builder.status == RequestBuilder.Status.STATUS_LOADING) {
                builder.setCancel(true);
            }
        }
    }

    public void clear() {
        RequestBuilder builderValue;
        for (Map.Entry<String, RequestBuilder> builder : builderMap.entrySet()) {
            builderValue = builder.getValue();
            //跳过正在加载的
            if (builderValue.status != RequestBuilder.Status.STATUS_LOADING) {
                builderMap.remove(builder.getKey());
            }
        }
    }


    public void load(Integer resId, Image component, ImageLoaderListener loaderListener) {
        loadResource(resId, component, null, loaderListener);
    }

    public void load(File file, Image component, ImageLoaderListener loaderListener) {
        loadResource(file, component, null, loaderListener);
    }

    public void load(String url, Image component, ImageLoaderListener loaderListener) {
        loadResource(url, component, null, loaderListener);
    }

    public void load(URL url, Image component, ImageLoaderListener loaderListener) {
        loadResource(url, component, null, loaderListener);
    }


    public void loadResource(Object obj, Image component, Integer placeholder, ImageLoaderListener loaderListener) {
        if (!(obj instanceof Integer)
                && !(obj instanceof String)
                && !(obj instanceof URI)
                && !(obj instanceof File)) {
            LogUtils.w("加载资源类型不正确,加载类型为 ResId/URI/File 中的一种");
            return;
        }
        if (component == null) {
            LogUtils.w("加载资源 Image 不能为空");
            return;
        }
        Context context = component.getContext();
        final RequestBuilder builder = get(context, obj);
        LogUtils.i("即将开始加载资源,RequestBuilder = %s", builder == null ? "null" : builder.toString());
        builder.loadResource(obj, component, placeholder,loaderListener);
    }

    private String getKey(Object obj) {
        String key = null;
        if (obj instanceof Integer) {
            key = obj + "";
        } else if (obj instanceof String) {
            key = (String) obj;
        } else if (obj instanceof URI) {
            key = ((URI) obj).toString();
        } else if (obj instanceof File) {
            key = ((File) obj).getAbsolutePath();
        }
        return key;
    }

    private RequestBuilder get(Context context, Object obj) {
        String key = getKey(obj);
        if (builderMap.containsKey(key)) {
            return builderMap.get(key);
        }
        if (null != key && key.length() > 0) {
            RequestBuilder builder = new RequestBuilder(context);
            builderMap.put(key, builder);
            return builder;
        }
        return null;
    }


}
